<?php

/**
 * 客商资料管理
 */

namespace App\Http\Controllers;

use App\Models\Company;
use App\Models\CompanyBusiness;
use App\Models\CompanyCategory;
use App\Models\CompanyContract;
use App\Models\AirInKefu;
use App\Models\AirInSell;
use App\Models\AirOutControler;
use App\Models\AirOutKefu;
use App\Models\AirOutSell;
use App\Models\GoodsCat;
use App\Models\SeaInKefu;
use App\Models\SeaInSell;
use App\Models\SeaOutKefu;
use App\Models\SeaOutSell;
use DB;
use Illuminate\Http\Request;

class GoodsCatController extends BaseController
{




    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $list = GoodsCat::all();
        return Success(10100,$list);
    }


    /**
     * 客商资料维护---公司新增
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request): \Illuminate\Http\JsonResponse
    {
        $name = $request->get("name");
        $sort_num = $request->get("sort_num");
        if (!$name || !$sort_num) {
            return Failed(10002, ["File" => "GoodsCatController", "line" => __LINE__, "need_filed" => ['name','sort_num']]);
        }
        $role = GoodsCat::where("name", $name)->first();
        if ($role) {
            return Failed(10006, ['File' => "GoodsCatController", "line" => __LINE__, "resource_same" => $name]);
        }
        $goodsCat = new GoodsCat();
        $goodsCat->name = $name;
        $goodsCat->sort_num = $sort_num;
        $goodsCat->status = 1;
        $goodsCat->updated_at = date("Y-m-d H:i:s");
        $goodsCat->created_at = date("Y-m-d H:i:s");
        if(!$goodsCat->save()){
            return Failed(10001, ['File' => "GoodsCatController", "line" => __LINE__]);
        }
        return Success(10000, []);
    }

    /**
     * 客商资料维护---公司编辑
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, int $id)
    {
        $name = $request->get("name");

        $sort_num = $request->get("sort_num");

        if (!$name || !$sort_num) {
            return Failed(10002, ["File" => "GoodsCatController", "line" => __LINE__, "need_filed" => ['name','sort_num']]);
        }

        $role = GoodsCat::where("name", $name)->first();
        if ($role) {
            return Failed(10006, ['File' => "GoodsCatController", "line" => __LINE__, "resource_same" => $name]);
        }

        if (!$id) {
            return Failed(10002, ["file" => "GoodsCatController", "line" => __LINE__, "need_filed" => ['id']]);
        }

        $goodsCat = GoodsCat::where([["id", $id]])->first();
        if (!$goodsCat) {
            return Failed(10005, ["file" => "GoodsCatController", "line" => __LINE__, "resources" => "not_found"]);
        }
        $goodsCat->name = $name;
        $goodsCat->sort_num = $sort_num;
        $goodsCat->status = 1;
        $goodsCat->updated_at = date("Y-m-d H:i:s");
        if(!$goodsCat->save()){
            return Failed(10001, ['File' => "GoodsCatController", "line" => __LINE__]);
        }
        return Success(11000, []);
    }

    /**
     * 客商资料维护---公司删除
     *
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(int $id): \Illuminate\Http\JsonResponse
    {
        //
        if (!$id) {
            return Failed(10002, ["File" => "GoodsCatController", "line" => __LINE__, "need_filed" => ['id']]);
        }
        $goodsCat = GoodsCat::where([["id", $id]])->first();
        if (!$goodsCat) {
            return Failed(10005, ["File" => "GoodsCatController", "line" => __LINE__, "resource" => "not_found"]);
        }

        if (!$goodsCat->delete()) {
            return Failed(10007, ["File" => "GoodsCatController", "line" => __LINE__, "resource" => "delete_failed"]);
        }
        return Success(10700, ["File" => "GoodsCatController", "line" => __LINE__, "resource" => "delete_success"]);
    }

    /**
     * 客商资料维护--公司相关人员新增
     * @param  Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function useradd(Request $request, int $id): \Illuminate\Http\JsonResponse
    {
        if (!$id) {
            return Failed(10002, ["File" => "CompanyController", "line" => __LINE__, "need_filed" => ['id']]);
        }
        $company = Company::where([["id", $id], ['status', 1]])->first();
        if (!$company) {
            return Failed(10005, ["File" => "CompanyController", "line" => __LINE__, "resource" => "not_found"]);
        }
        $model = $request->get("model");
        $user_id = $request->get("user_id");
        if (!$model || !$user_id) {
            return Failed(10002, [
                "file" => "CompanyController", "line" => __LINE__,
                "need_filed" => ['model', 'user_id']
            ]);
        }
        if (!in_array($model, $this->allow_model)) {
            return Failed(10301, [
                "file" => "CompanyController", "line" => __LINE__,
                "allow_model_filed" => $this->allow_model
            ]);
        }
        $data = [
            "company_id" => $id,
            "user_id" => $user_id
        ];

        $res = $this->model[$model]::where([["company_id", $id]])->updateOrCreate($data);
        if (!$res) {
            return Failed(10002,
                ["File" => "CompanyController", "line" => __LINE__, "updated_error" => "添加相关人员失败"]);
        }
        return Success(11000,
            ["File" => "CompanyController", "line" => __LINE__, "updated_success" => "添加相关人员成功"]);
    }

    /**
     * 客商资料维护--公司相关人员删除
     * @param  Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function userdel(Request $request, int $id): \Illuminate\Http\JsonResponse
    {
        if (!$id) {
            return Failed(10002, ["File" => "CompanyController", "line" => __LINE__, "need_filed" => ['id']]);
        }
        $company = Company::where([["id", $id], ['status', 1]])->first();
        if (!$company) {
            return Failed(10005, ["File" => "CompanyController", "line" => __LINE__, "resource" => "not_found"]);
        }
        $model = $request->get("model");
        $user_id = $request->get("user_id");
        if (!$model || !$user_id) {
            return Failed(10002, [
                "file" => "CompanyController", "line" => __LINE__,
                "need_filed" => ['model', 'user_id']
            ]);
        }
        if (!in_array($model, $this->allow_model)) {
            return Failed(10301, [
                "file" => "CompanyController", "line" => __LINE__,
                "allow_model_filed" => $this->allow_model
            ]);
        }
        $res = $this->model[$model]::where([["company_id", $id],['user_id',$user_id]])->delete();
        if (!$res) {
            return Failed(10007,
                ["File" => "CompanyController", "line" => __LINE__, "resources" => "delete_failed"]);
        }
        return Success(10700,
            ["File" => "CompanyController", "line" => __LINE__, "resources" => "delete_success"]);
    }

    /**
     * 客商资料维护--公司类别编辑
     * @param  Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function category(Request $request, int $id): \Illuminate\Http\JsonResponse
    {
        if (!$id) {
            return Failed(10002, ["File" => "CompanyController", "line" => __LINE__, "need_filed" => ['id']]);
        }
        $company = Company::where([["id", $id], ['status', 1]])->first();
        if (!$company) {
            return Failed(10005, ["File" => "CompanyController", "line" => __LINE__, "resource" => "not_found"]);
        }
        $categoryArr = $request->get("categoryArr");

        if (empty($categoryArr)) {
            return Failed(10002, [
                "file" => "CompanyController", "line" => __LINE__,
                "need_filed" => ['categoryArr']
            ]);
        }
        if(!empty(CompanyCategory::where([["company_id", $id]])->get()->toArray())){
            $res = CompanyCategory::where([["company_id", $id]])->delete();
            if (!$res) {
                return Failed(10007,
                    ["File" => "CompanyController", "line" => __LINE__, "resources" => "delete_failed","msg"=>"公司类别删除失败"]);
            }
        }

        $data = [];
        foreach ($categoryArr as $vo){
            $data[]=[
                "company_id"=>$id,
                "category_id"=>$vo
            ];
        }

        if(!CompanyCategory::insert($data)){
            return Failed(10002,
                ["File" => "CompanyController", "line" => __LINE__, "updated_error" => "公司类别编辑失败"]);
        }
        return Success(11000,
            ["File" => "CompanyController", "line" => __LINE__, "updated_success" => "公司类别编辑成功"]);
    }

    /**
     * 保存 公司业务范围
     * @param  array  $business_arr
     * @param $company
     * @return true
     */
    public function BusinessScope(array $business_arr, $company): bool
    {
        foreach ($business_arr as $vo) {
            CompanyBusiness::create([
                "company_id" => $company->id,
                "business_id" => $vo
            ]);
        }
        return true;
    }

    public function getCompanyIdByUserId($user_id)
    {
        if (!$user_id) {
            return false;
        }

    }

}
